package com.test.designPattern.ch20;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName Subject
 * @Description TODO
 * @Author 维康
 * @Date 2018/6/13 10:40
 * @Version 1.0
 **/
public class Subject {

    //保存所有的观察者，一旦发生变化，通知所有的观察者
    private List<Observer> observers = new ArrayList<Observer>();
    private int state;

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
        notifyAllObservers();
    }

    public void attach(Observer observer){
        observers.add(observer);
    }

    /**
     * 通知观察者
     */
    public void notifyAllObservers(){
        for (Observer observer : observers) {
            observer.update();
        }
    }
}
